//
//  ARAudioPlayDefault.m
//  AriseSamePro
//
//  Created by 包日守 on 15/10/30.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "ARAudioPlayDefault.h"
#import "ARGlobalInterface.h"

@implementation ARAudioPlayDefault
+ (id) defaultAudioPlay
{
    if (audioinstance == nil) {
        
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            audioinstance = [super alloc];
            audioinstance = [audioinstance init];
            audioinstance.arrayCates = [NSMutableArray array];
            
        });

        
        
    }
    return audioinstance;

}

- (ARCate5Model *)getCateModelInCacheWithPath:(NSString *) path
{
    for (ARCate5Model *cateModel in self.arrayCates) {
        if ([path isEqualToString:[ARGlobalInterface musicTempCachePathWithMusicPath:cateModel.mediaModel.musicModel.src]]) {
            return cateModel;
            
        }
        
    }
    return nil;

}


- (void) addCateModel:(ARCate5Model *) cateModel
{
    for (ARCate5Model *model in self.arrayCates) {
        if ([model.index isEqualToString:cateModel.index]) {
            return;
        }
    }
    [self.arrayCates addObject:cateModel];
    
}

- (void) toNextSong
{
    NSArray *paths = [ARGlobalInterface tempMusicInCache];
    NSString *lastPath ;
    int lastIndex = 0;
    //NSLog(@"%@",paths[0]);
    //NSLog(@"%@",audioDefault.MusicPath);
    BOOL isFind = NO;
    for (int i = 0; i < paths.count; i++)
    {
        NSString *path = paths[i];
        if ([self.MusicPath isEqualToString:path]) {
            lastIndex = i;
            isFind = YES;
            break;
            
        }
        
    }
    if (!isFind) {
        return;
    }
    
    lastIndex = (++lastIndex==self.arrayCates.count)?0:lastIndex;
    lastPath = paths[lastIndex];
    
    [self.player stop];
    
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:lastPath] error:nil];
    //[self.player prepareToPlay];
    [self.player setVolume:1];
    self.player.numberOfLoops = -1;
    self.player.currentTime = 0;
    
    [self.player play];
    [self.timer invalidate];
    
    ARCate5Model *cateModel = [self getCateModelInCacheWithPath:lastPath];
    
    self.isPlay = YES;
    self.playingCate = cateModel;
    self.duration = [NSNumber numberWithInteger:self.player.duration];
    self.title = cateModel.mediaModel.musicModel.title;
    self.author = cateModel.mediaModel.musicModel.author;
    self.MusicPath = lastPath;

}
- (void) toLastSong
{
    NSArray *paths = [ARGlobalInterface tempMusicInCache];
    NSString *lastPath ;
    int lastIndex = 0;

    BOOL isFind = NO;
    for (int i = 0; i < paths.count; i++)
    {
        NSString *path = paths[i];
        if ([self.MusicPath isEqualToString:path]) {
            lastIndex = i;
            isFind = YES;
            break;
            
        }
        
    }
    if (!isFind) {
        return;
    }
    
    lastIndex = (--lastIndex==-1)?(int)paths.count - 1:lastIndex;
    lastPath = paths[lastIndex];
    
    [self.player stop];
    [self.timer invalidate];
    
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:lastPath] error:nil];
    //[self.player prepareToPlay];
    [self.player setVolume:1];
    self.player.numberOfLoops = -1;
    self.player.currentTime = 0;
    [self.player play];
    
    
    ARCate5Model *cateModel = [self getCateModelInCacheWithPath:lastPath];
    
    self.isPlay = YES;
    self.playingCate = cateModel;
    self.duration = [NSNumber numberWithInteger:self.player.duration];
    self.title = cateModel.mediaModel.musicModel.title;
    self.author = cateModel.mediaModel.musicModel.author;
    self.MusicPath = lastPath;

}

- (void) startMusic
{
    self.isPlay = YES;
    [self.player play];

}

- (void) pauseMusic
{
    self.isPlay = NO;
    [self.player pause];

}


@end
